import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        E.right=H;
        C.left=F;
        C.right=G;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder(TreeNode root){

        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    public void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        inOrder(root.right);
        System.out.print(root.val+" ");

    }

    public static int nodeSize=0;
    // 获取树中节点的个数
    public int size(TreeNode root){
        if(root==null){
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }
    // 获取树中节点的个数
    public int size1(TreeNode root){
        if(root==null){
            return 0;
        }
        return size1(root.left)+size1(root.right)+1;
    }
    public static int leafNode;
    // 获取叶子节点的个数
    public void getLeafNodeCount(TreeNode root){
        if(root==null){
            return;
        }
        if(root.right==null&& root.left==null){
            leafNode++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    // 获取叶子节点的个数
    public int getLeafNodeCount1(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.right==null&& root.left==null){
            return 1;
        }
        return getLeafNodeCount1(root.left)+getLeafNodeCount1(root.right);
    }
    // 子问题思路-求叶子结点个数
// 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root==null) return 0;
        if(k==1) return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);

        return leftHeight>=rightHeight?leftHeight+1:rightHeight+1;
    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode leftN=find(root.left,val);
        if(leftN.val==val){
            return leftN;
        }
        TreeNode rightN=find(root.left,val);
        if(rightN.val==val){
            return rightN;
        }
        return null;
    }
    //层序遍历
    public void levelOrder(TreeNode root){

        if(root==null){
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        TreeNode cur=root;
        queue.offer(root);
        while(!queue.isEmpty()){
            cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        TreeNode cur=root;
        queue.offer(root);
        while(!queue.isEmpty()){
            cur=queue.poll();
            if(cur==null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        if(queue.size()==0){
            return true;
        }
        for (int i = 0; i <queue.size() ; i++) {
            if(queue.poll()!=null){
                return false;
            }
        }
        return true;
    }

    //判断相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p!=null&&q!=null){
            if(p.val==q.val){
                boolean a=isSameTree(p.left,q.left);
                boolean b=isSameTree(p.right,q.right);
                return a&&b;
            }else{
                return false;
            }
        }
        if(p==null&&q==null){
            return true;
        }
        return false;
    }
    //判断一棵树为另一颗树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root==null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }

        if(isSubtree(root.left,subRoot)){
            return true;
        }
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }

    //反转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode tmp=root.left;
        root.left=root.right;
        root.right=tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }

        return is(root.left,root.right);
    }

    public boolean is(TreeNode leftTree,TreeNode rightTree){
        if(leftTree==null&&rightTree!=null ||leftTree!=null&&rightTree==null){
            return false;
        }
        if(leftTree==null&&rightTree==null){
            return true;
        }
        if(leftTree.val!=rightTree.val){
            return false;
        }

        return is(leftTree.left,rightTree.right) && is(leftTree.right,rightTree.left);
    }


    //判断是否为平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int height1=height(root.left);
        int height2=height(root.right);

        return Math.abs(height1-height2)<2&&isBalanced(root.left)&&isBalanced(root.right);
    }

    public int height(TreeNode root) {
        if(root==null){
            return 0;
        }
        int height1=height(root.left);
        int height2=height(root.right);
        return Math.max(height1,height2)+1;
    }


    //二叉树层序遍历
    public List<List<Integer>> levelOrder1(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        TreeNode cur=root;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size=queue.size();
            List<Integer> list=new ArrayList<>();
            while(size!=0) {
                cur = queue.poll();
//                list.add(cur.val);
                //System.out.print(cur.val + " ");
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }
}
